#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Blueprint,render_template, request, session
from ..database import Article, Comment,User,db
from ..utils import prettify
from ..secure import login_required

bp = Blueprint('ajax_handle', __name__, url_prefix="/ajax_handle")

@bp.route('/', methods=('POST',))
def view_ajax_handle():

   
    method = request.values.get('handle')
    post_id = request.values.get('post_id')
    
    if method == 'comment':
        # 获取评论内容,和评论人
        comment = request.values.get('comment')
        who = session['user']['name']
        user_id=User.query.filter_by(name=who).first().id
        comm = Comment(
            id=None,
            comment=comment,
            name_id=user_id,
            article_id=post_id
            )
        db.session.add(comm)
        db.session.commit()


    elif method == 'mark':
        num = int(request.values.get('num'))
        article = Article.query.filter_by(id=post_id).first()
        
        article.bookmark_num = num+1
        db.session.commit()
    
    elif method == 'point':
        num = int(request.values.get('num'))
        article = Article.query.filter_by(id=post_id).first()
        
        article.point_up_num = num+1
        db.session.commit()
       
    
    return 'ok'
    
